package com.example.demo.outlook;

import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.microsoft.aad.msal4j.ClientCredentialFactory;
import com.microsoft.aad.msal4j.ClientCredentialParameters;
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
import com.microsoft.aad.msal4j.IAuthenticationResult;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.models.Message;
import com.microsoft.graph.models.Recipient;
import com.microsoft.graph.models.User;
import com.microsoft.graph.requests.ApplicationCollectionRequestBuilder;
import com.microsoft.graph.requests.GraphServiceClient;
import com.microsoft.graph.requests.MessageCollectionPage;
import com.microsoft.graph.requests.PersonCollectionPage;
import com.nimbusds.oauth2.sdk.http.HTTPResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;


public class OutlookDemo {

    private static String authority = "https://login.microsoftonline.com/aaf1697c-4473-4326-bd03-7d25effca28b/";
    private static String tenantId = "aaf1697c-4473-4326-bd03-7d25effca28b";
    private static String clientId = "6169a47e-3a2e-4c47-87ac-d91acc524a20";
    private static String secret = "WOP8Q~noPAxMZBAek.nI4_kuLVWUA_h93Eh0nbNw";
    private static String accessToken = "";
    private static String mailbox = "caad@chint.com";

    private static List<String> scope = Arrays.asList("https://graph.microsoft.com/.default".split(","));

    private static final ClientSecretCredential clientSecretCredential = null;

    private static final TokenCredentialAuthProvider tokenCredAuthProvider = null;

    private static final GraphServiceClient graphClient = null;

    public static void main(String args[]) throws Exception {

        System.out.println("hello world");
    }
        /*
        MessageCollectionPage messages = GraphUtil.listMessages(graphClient, mailbox);

        for (Message message:messages.getCurrentPage()){
            System.out.println("Id--->"+message.id);
            String senderName = message.from.emailAddress.name;
            String senderAddress = message.from.emailAddress.address;
            String subject = message.subject;
            String importance = message.importance.name();
            String webLink = message.webLink;
            String bodyContent = message.body.content;
            LocalDateTime receivedDateTime = message.receivedDateTime.toLocalDateTime();
            LocalDateTime sentDateTime = message.sentDateTime.toLocalDateTime();
            List<Recipient> toRecipients  = message.toRecipients;
            System.out.println(toRecipients.listIterator().next().emailAddress.address);

        }


    }


    public static Message getMessageById(GraphServiceClient graphClient,String mailbox,String mailId) throws IOException {
        Message message = graphClient.users(mailbox).messages(mailId)
                .buildRequest()
                .get();
        return message;
    }

    public static String getAccessToken() throws Exception {
        ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder().clientId(clientId)
                .tenantId(tenantId).clientSecret(secret).build();

        TokenCredentialAuthProvider tokenCredAuthProvider = new TokenCredentialAuthProvider(scope,
                clientSecretCredential);

        GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(tokenCredAuthProvider)
                .buildClient();

        IAuthenticationResult result = getAccessTokenByClientCredentialGrant();
        return accessToken = result.accessToken();

    }

    public static MessageCollectionPage listMessages(GraphServiceClient graphClient,String mailbox) throws IOException {
        MessageCollectionPage messages = graphClient.users(mailbox).messages()
                .buildRequest()
                .get();
        return messages;
    }

    public static IAuthenticationResult getAccessTokenByClientCredentialGrant() throws Exception {

        ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                        clientId,
                        ClientCredentialFactory.createFromSecret(secret))
                .authority(authority)
                .build();

        // With client credentials flows the scope is ALWAYS of the shape "resource/.default", as the
        // application permissions need to be set statically (in the portal), and then granted by a tenant administrator
        ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                        Collections.singleton(scope.toString()))
                .build();

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
        return future.get();

    }


         */

}


